
#include "darkfullcoverbullet.h"
#include "resourcemanager.h"
#include "gamemanager.h"
#include "frameanimation.h"
#include "CCSkeletalSprite.h"
#include "gamedatamanager.h"
#include "army.h"
#include "unit.h"
#include "gamescene.h"

#define __super         Bullet

DarkFullCoverBullet::DarkFullCoverBullet(
		Army *army1,
		Army *army2,
		EquipmentAttribute *attr,
		GameScene *scene):
__super(attr)
, _gameScene(scene)
, _army1(army1)
, _army2(army2)
, _delay(0.0f) {
}

DarkFullCoverBullet::~DarkFullCoverBullet() {
}

void DarkFullCoverBullet::init() {
	ManagedGameNodeInfo info;
	for (int i = 0; i < TOTAL_LINES; i++) {
		for (int j = 0; j < ROWS_PER_LINE; j++) {
			Unit *unit = _army1->soldiers[i][j];
			if (unit && unit->attribute.genre != EquipmentGenre::creature_demon) {
				FrameAnimation *fire = RM->createAnimation("fx_dark_hit", unit->position);
				fire->setLoop(false);

				info.node = fire;
				_gameScene->addOneTimeNode(info);
			}

			unit = _army2->soldiers[i][j];
			if (unit && unit->attribute.genre != EquipmentGenre::creature_demon) {
				FrameAnimation *fire = RM->createAnimation("fx_dark_hit", unit->position);
				fire->setLoop(false);

				info.node = fire;
				_gameScene->addOneTimeNode(info);
			}
		}
	}

	if (_delay == 0.0f) {
		auto info = RM->getAnimationInfo("fx_dark_hit");
		_delay = info.frameRate * info.frames.size();
	}
}

bool DarkFullCoverBullet::update(float ticks) {
	bool ret = true;
	_delay -= ticks;
	if (_delay <= 0.0f) {
		applyBulletEffect();

		ret = false;
	}

	return ret;
}

void DarkFullCoverBullet::applyBulletEffect() {
	for (int i = 0; i < TOTAL_LINES; i++) {
		for (Unit *unit :_army1->soldiers[i]) {
			if (unit) {
				unit->applyBullet(this);
			}
		}
		for (Unit *unit :_army2->soldiers[i]) {
			if (unit) {
				unit->applyBullet(this);
			}
		}
	}
}
